package com.guigu.controller.design;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.pojo.design.DFile;
import com.guigu.pojo.design.DModule;
import com.guigu.pojo.design.DModuleDetails;
import com.guigu.pojo.design.DProductUse;
import com.guigu.service.design.DFileService;
import com.guigu.service.design.DModuleDetailsService;
import com.guigu.service.design.DModuleService;
import com.guigu.service.design.DProductUseService;
import com.guigu.utils.CodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Controller
@RequestMapping("/d-module")
public class DModuleController {

    @Resource
    private DFileService dFileService;

    @Autowired
    private DModuleService dModuleService;

    @Autowired
    private DModuleDetailsService dModuleDetailsService;

    @Autowired
    private DProductUseService dProductUseService;

    //物料组成设计单查询
    @GetMapping("/formulate_query")
    public String goFormulateQuery(){
        return "jsp/design/Formulate_design_composition_query";
    }

    //物料组成设计单变更
    @GetMapping("/design_change")
    public String goDesignChange(){
        return "jsp/design/design_Composition_Change_query";
    }

    //物料组成设计单审核
    @GetMapping("/design_review")
    public String goDesignReview(){
        return "jsp/design/design_composition_review";
    }

    @GetMapping("/FormulateMaterialCompositionDesign")
    public String goFormulateMaterialCompositionDesign(){
        return "jsp/design/Formulate_Material_Composition_Design";
    }

    @GetMapping("/getMaterialById")
    @ResponseBody
    public DFile getMaterialById(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        DFile dFile = dFileService.selectById(id);
        DProductUse dProductUse = dProductUseService.selectByFileId(dFile);
        dFile.setDProductUse(dProductUse);
        return dFile;
    }

    @PostMapping("/designFormulate")
    @ResponseBody
    public Boolean designFormulate(@RequestBody ArrayList<Object> arr){
        System.out.println(arr);
        String productId ="";
        String productName ="";
        String designer = "";
        String register = "";
        String registerTime = "";
        String moduleDescribe = "";

        HashMap<String, Object> map = (HashMap<String, Object>) arr.get(0);
        if(map.get("productId")!=null){
            productId = map.get("productId").toString();
        }
        if(map.get("productName")!=null){
            productName = map.get("productName").toString();
        }
        if(map.get("designer")!=null){
            designer = map.get("designer").toString();
        }
        if(map.get("register")!=null){
            register = map.get("register").toString();
        }
        if(map.get("registerTime")!=null){
            registerTime = map.get("registerTime").toString();
        }
        if(map.get("moduleDescribe")!=null){
            moduleDescribe = map.get("moduleDescribe").toString();
        }

        DFile dFile = dFileService.selectByProductId(productId);

        //获取物料组成明细
        ArrayList<DModuleDetails> details = new ArrayList<>();
        //获取物料总成本
        BigDecimal costPriceSum = BigDecimal.valueOf(0L);
        //获取物料档案
        for (int i = 1; i < arr.size(); i++) {
            HashMap<String, Object> map1 = (HashMap<String, Object>) arr.get(i);
            DFile dFile1 = dFileService.selectByProductId(map1.get("id").toString());//获取产品档案
            DModuleDetails dModuleDetails = new DModuleDetails();
            dModuleDetails.setParentId(null);
            dModuleDetails.setDetailsNumber(i);
            dModuleDetails.setProductId(dFile1.getProductId());
            dModuleDetails.setProductName(dFile1.getProductName());
            dModuleDetails.setType(dFile1.getType());
            dModuleDetails.setProductDescribe(dFile1.getProductDescribe());
            dModuleDetails.setAmountUnit(dFile1.getAmountUnit());
            dModuleDetails.setAmount(BigDecimal.valueOf(Long.parseLong(map1.get("num").toString())));//数量
            dModuleDetails.setResidualAmount(BigDecimal.valueOf(0L));
            dModuleDetails.setCostPrice(dFile1.getCostPrice());
            BigDecimal Subtotal = BigDecimal.valueOf(Long.parseLong(map1.get("num").toString()) * dFile1.getCostPrice().longValue());//子总价
            dModuleDetails.setSubtotal(Subtotal);//总价
            costPriceSum = BigDecimal.valueOf(costPriceSum.longValue() + Subtotal.longValue());
            details.add(dModuleDetails);
        }

        DModule dModule = new DModule();
        DModule endModule = dModuleService.getEndModule();
        CodeUtils codeUtils = new CodeUtils();
        String materialCode = codeUtils.getMaterialCode(endModule);

        dModule.setDesignId(materialCode);//设计单编号
        dModule.setProductId(productId);
        dModule.setProductName(productName);
        dModule.setFirstKindId(dFile.getFirstKindId());
        dModule.setFirstKindName(dFile.getFirstKindName());
        dModule.setSecondKindId(dFile.getSecondKindId());
        dModule.setSecondKindName(dFile.getSecondKindName());
        dModule.setThirdKindId(dFile.getThirdKindId());
        dModule.setThirdKindName(dFile.getThirdKindName());
        dModule.setDesigner(designer);
        dModule.setModuleDescribe(moduleDescribe);
        dModule.setCostPriceSum(costPriceSum);
        dModule.setRegister(register);
        dModule.setRegisterTime(new Date());
        dModule.setChecker(null);
        dModule.setCheckTime(null);
        dModule.setCheckTag("0");
        dModule.setChangeTag("0");
        dModule.setChangeTime(null);
        dModule.setChanger(null);

        int id = dModuleService.addModule(dModule);
        for (DModuleDetails dModuleDetails : details) {
            dModuleDetails.setParentId(id);
            dModuleDetailsService.save(dModuleDetails);
        }
        dFile.setDesignModuleTag("1");
        boolean b = dFileService.updateById(dFile);
        return b;
    }

    @PostMapping("/initialMaterialApprovalTable")
    @ResponseBody
    public PageInfo<DModule> initialMaterialApprovalTable(@RequestBody HashMap<String,Object> map){
        PageInfo<DModule> dModulePageInfo = dModuleService.selectByCheckTag(map);
        return dModulePageInfo;
    }

    @PostMapping("/initialMaterialDesignTable")
    @ResponseBody
    public PageInfo<DModule> initialMaterialDesignTable(@RequestBody HashMap<String, Object> map) {
        PageInfo<DModule> dModulePage = dModuleService.selectModuleByPage(map);
        return dModulePage;
    }

}

